<?php

namespace app\api\controller;

use app\api\model\UserHistory as UserHistoryModel;
use app\common\controller\Api;
use app\api\model\User as UserModel;
use think\Db;
use app\api\model\Goods;

class Userhistory extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 足迹列表
     */
    public function historylist()
    {
        $uid = $this->auth->id;
        $page  = $this->request->param('page',1);
        $limit = $this->request->param('limit',50);
        $name  = $this->request->param('name','');
        $UserHistoryModel = new UserHistoryModel();

        $response = $UserHistoryModel->hislist($uid,$page,$limit,$name);

        if ($response['count'] > 0)
        {
            $this->success('获取成功', $response, 200);
        }else{
            $this->success('暂无浏览记录', $response, 200);
        }
    }

    /**
     * 清空记录
     */
    public function historydel()
    {
        $uid = $this->auth->id;
        $id = $this->request->param('id');
        $data = UserHistoryModel::destroy(function ($query) use ($uid,$id){
            $where = [];
            if ($id)
            {
                $where['id'] = ['in',$id];
            }
            $query->where('uid',$uid)->where($where);
        });
        if ($data)
        {
            $this->success('清除成功',$data,200);
        }else{
            $this->success('清除失败','',400);
        }
    }
}
